package com.think.common.security.convert;

import org.springframework.core.convert.converter.Converter;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Component;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;


public class StringToTimestampConverter implements Converter<String, Timestamp> {

    private final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

    @Override
    public Timestamp convert(String source) {
        try {
            Date date = dateFormat.parse(source);
            return new Timestamp(date.getTime());
        } catch (ParseException e) {
            throw new IllegalArgumentException("Invalid date format. Please use this pattern: " + dateFormat.toPattern());
        }
    }
}
